<?php
/**
 * 商品分类模型层
 * 
 * @copyright 	Copyright (c) 2011 thinkapk.com All rights reserved.
 * @package 	ThinkAPK_Model
 * @link 		http://www.thinkapk.com/
 * @author 		richie <richbossman@163.com>
 * @version 	$Id: CategoryModel.class.php 7494 2011-05-24 10:03:05Z richie $
 * 
 */
class CategoryModel extends CommonModel {
	
	/**
	 * 取得商品分类列表
	 *
	 * @param int $pid
	 * @param bool	$showad
	 * @return array
	 */
	public function get($pid=0, $app='app'){
		$cates = $this->where("is_show=1 AND parent_id=$pid")->order('sort_order ASC')->field('cat_id as id, cat_name as name,parent_id')->findAll();
		if($cates){
			foreach($cates as $key=>$cat){
				if(in_array(strtolower($app), array('app', 'game'))){
					if($cat['parent_id'] == 0){
						if($cat['id'] == C('APP_ID')){
							$cates[$key]['url'] = U('thinkapk://App/index');
						}else{
							$cates[$key]['url'] = U('thinkapk://Game/index');
						}
					}elseif($cat['parent_id'] == C('APP_ID')){
						$cates[$key]['url'] = U('thinkapk://App/index', array('id'=>$cat['id']));
					}elseif($cat['parent_id'] == C('GAME_ID')){
						$cates[$key]['url'] = U('thinkapk://Game/index', array('id'=>$cat['id']));
					}else{
						$cates[$key]['url'] = U('thinkapk://'.$app.'/index', array('id'=>$cat['id']));
					}
				}else	$cates[$key]['url'] = U('thinkapk://'.$app.'/index', array('id'=>$cat['id']));
				$children = $this->get($cat['id'], $app);
				if($children) $cates[$key]['children'] = $children;
			}
		}
		return $cates;
	}
	
	//销售排行榜总榜数据
	public function getTops(){
		$cat = $this->get(0, 'top');
		foreach($cat as $key=>$parent){
			$cat[$key]['android'] = D('GoodsCount')->get($parent['id']);
			if(isset($parent['children']) && !empty($parent['children'])){
				foreach ($parent['children'] as $ck=>$child){
					$cat[$key]['children'][$ck]['android'] = D('GoodsCount')->get($child['id']);
				}
			}
		}
		return $cat;
	}
	
	/**
	 * 商品分类详细信息
	 *
	 * @param int $id
	 * @return array
	 */
	public function getCategory($id=0){
		if(empty($id)) return array();
		return $this->find($id);
	}
	
	/**
	 * 取得当前分类路径
	 *
	 * @param int $id
	 * @return array
	 */
	public function getParentArray($id=0, $action='app'){
		static $array;
		if($id == 0){
			return array();
		}
		$parent = $this->field('cat_id as id, cat_name as name, parent_id')->where(array('cat_id'=>$id))->find();
		if($parent){
			$parent['url'] = U('thinkapk://'.$action.'/index', array('id'=>$id));
			if($id == C('APP_ID')){
				$parent['url'] = U('thinkapk://app/index');
			}elseif($id == C('GAME_ID')){
				$parent['url'] = U('thinkapk://game/index');
			}
			$array[] = $parent;
			if($parent['parent_id']){
				$this->getParentArray($parent['parent_id'], $action);	
			}
		}
		return array_reverse($array);
	}
	
	/**
	 * 取得指定分类的所有下级分类ID
	 *
	 * @param int $id
	 * @return array
	 */
	public function getChiledrenId($id=0, $me=false, $childs=array()){
		if($id == 0) return array();
		$childId = $this->where(array('parent_id'=>$id))->field('cat_id')->findAll();
		if($childId){
			foreach ($childId as $child){
				$childs[] = $child['cat_id'];
				$this->getChiledrenId($child['cat_id'], true, $childs);
			}
		}
		if($me === false) $childs[] = $id;
		return $childs;
	}
	
	/**
	 * 取得指定分类的顶级分类ID
	 *
	 * @param int $id
	 * @return int
	 */
	public function getParentId($id=0){
		$parent = $this->where(array('cat_id'=>$id))->getField('parent_id');
		if($parent == 0){
			return $id;
		}else{
			return $this->getParentId($parent);
		}
	}
	
	/**
	 * 取得指定分类的级别，属于第几级分类
	 *
	 * @param int $id
	 * @return int
	 */
	public function getLevel($id=0, $level=1){
		$parent = $this->where(array('cat_id'=>$id))->getField('parent_id');
		if($parent == 0){
			return $level;
		}else{
			$level++;
			return $this->getLevel($parent, $level);
		}		
	}
	
	/**
	 * 取得指定ID组的分类名称与链接
	 *
	 * @param string $ids
	 * @return array
	 */
	public function getCategoryByIds($ids=''){
		$map['cat_id'] = array('IN', $ids);
		$map['is_show'] = 1;
		$category = $this->where($map)->field('cat_id as id, cat_name as name')->findAll();
		if($category){
			foreach ($category as $key=>$cate){
				$category[$key]['url'] = U('mall://List/index', array('id'=>$cate['id']));
			}
		}
		return $category;
	}
	
	//取得首页推荐的ID
	public function getIndexRecommendCat($parent_id=0){
		return $this->where(array('parent_id'=>$parent_id, 'is_show'=>1, 'show_in_nav'=>1))->field('cat_id as id, cat_name as name')->order('sort_order ASC, cat_id DESC')->findAll();
	}
	
	//取得子分类
	public function getAppCategory($id=0, $type='app'){
		$row = $this->where(array('parent_id'=>$id, 'is_show'=>1))->field('cat_id as id, cat_name as name')->order('sort_order ASC, cat_id DESC')->findAll();
		if($row){
			foreach ($row as $key=>$app){
				$row[$key]['url'] = U('thinkapk://'.$type.'/index', array('id'=>$app['id']));
				$row[$key]['total'] = D('Goods')->where(array('cat_id'=>$app['id'], 'is_delete'=>0, 'is_on_sale'=>1))->count();
			}
		}
		return $row;
	}
}
?>